﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Pub;
using System.Threading;

namespace LZQXT.Test.Forms.Cmd
{
    public partial class FCmd : Form
    {
        public delegate void tboxCallBack(string sTxt);
        string sCmd = "";
        Thread thread1;

        public FCmd()
        {
            InitializeComponent();
        }

        public void tboxAppend(string sTxt)
        {
            if (tboxResult.InvokeRequired)
            {
                tboxCallBack tbox_CB = new tboxCallBack(tboxAppend);
                this.Invoke(tbox_CB, new object[] { sTxt });
            }
            else
            {
                tboxResult.AppendText(sTxt);
                tboxResult.SelectionStart = tboxResult.TextLength;
                tboxResult.ScrollToCaret();
            }
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            if (cboxCmd.Text != "")
            {
                try
                {
                    //保存输入的命令
                    Boolean b_has = false;
                    for (int i = 0; i < cboxCmd.Items.Count; i++)
                    {
                        if (cboxCmd.Items[i].ToString() == cboxCmd.Text)
                        {
                            b_has = true;
                            break;
                        }
                    }

                    if (b_has == false) cboxCmd.Items.Add(cboxCmd.Text);

                    sCmd = cboxCmd.Text;
                    cboxCmd.Text = "";

                    if (sCmd.ToLower() == "cls")
                    {
                        tboxResult.Text = "";
                    }
                    else
                    {
                        if (thread1 != null) thread1.Abort();
                        thread1 = new Thread(new ThreadStart(StartCmd));
                        thread1.Start();
                    }


                    //StartCmd();
                }
                catch (System.Exception ex)
                {
                    CDebug.SendError(ex.ToString());
                }


            }
        }

        public void StartCmd()
        {
            //string s1 = CCmd.startcmd(sCmd);
            string s1 = CCmd.Cmd(sCmd);
            //string s1 = CCmd.RunScript(sCmd);
            tboxAppend(s1);
        }

        private void FCmd_Load(object sender, EventArgs e)
        {
            tboxResult.Dock = DockStyle.Fill;
            //CTextEditMenu menu1 = new CTextEditMenu(tboxResult);
            CRichTextBoxMenu menu1 = new CRichTextBoxMenu(tboxResult);
        }

        private void cboxCmd_KeyDown(object sender, KeyEventArgs e)
        {
            if (cboxCmd.Text != "")
            {
                if (e.KeyCode == Keys.Return)
                {
                    btnRun_Click(btnRun, null);
                }
            }
        }

        private void FCmd_FormClosing(object sender, FormClosingEventArgs e)
        {
            //终止线程
            thread1.Abort();
        }
    }

    public static class JTBOX
    {
        /// <summary>
        /// 注册插件
        /// </summary>
        /// <param name="node1"></param>
        /// <param name="list1"></param>
        /// <returns></returns>
        public static bool RegPlug(List<Form> list1, ImageList imgLst)
        {
            try
            {
                FCmd f1 = new FCmd();
                list1.Add(f1);

                imgLst.Images[list1.Count - 1] = f1.imageList2.Images[0];
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
                return false;
            }
            return true;
        }
    }
}
